//
// @Auther: by wujehy 
// @Email:wujehy@qq.com
// @Data:2019/10/08
// @Time:11:09 上午
//
//

#include <string>
#include <iostream>
#include <mosquitto_broker.h>
#include <cstring>
#include "Logger.h"
#include "Common.h"

void Logger::LogToConsole(Logger::LogType type,const std::string &logBuffer)
{

#ifdef BUILD_DEBUG
#else
    //非 Debug 模式直接 结束
    if (type == LogType::Debug)
    {
        return;
    }
#endif
    std::string logString;
    int64_t currentTime = Common::getTimeMilli();
    switch (type)
    {
        case Debug:
            logString.append("[Debug][");
            break;
        case Error:
            logString.append("[Error][");
            break;
        case Info:
            logString.append("[Info][");
            break;
        case Warning:
            logString.append("[Warning][");

            break;
        default:
            break;
    }
    logString.append(std::to_string(currentTime));
    logString.append("][");
    logString.append(stream->fileName);
    logString.append(":");
    logString.append(std::to_string(stream->lineNub));
    logString.append("][");
    logString.append(stream->funcName);
    logString.append("]:");
    logString.append(logBuffer);

    if (type == Error){
        std::cerr<<logString << std::endl;
    } else{
        std::cout << logString << std::endl;
    }

}

Logger::~Logger()
{
    this->LogToConsole(stream->logType, stream->ss.str());
    delete stream;
}
